package net.lab1024.sa.admin.module.business.roland.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.roland.dao.RolandCustomerDao;
import net.lab1024.sa.admin.module.business.roland.dao.RolandOrderDao;
import net.lab1024.sa.admin.module.business.roland.dao.RolandPerformanceStudentDao;
import net.lab1024.sa.admin.module.business.roland.domain.entity.RolandCustomerEntity;
import net.lab1024.sa.admin.module.business.roland.domain.entity.RolandOrderEntity;
import net.lab1024.sa.admin.module.business.roland.domain.entity.RolandPerformanceStudentEntity;
import net.lab1024.sa.admin.module.business.roland.domain.form.RolandCustomerAddForm;
import net.lab1024.sa.admin.module.business.roland.domain.form.RolandCustomerQueryForm;
import net.lab1024.sa.admin.module.business.roland.domain.form.RolandCustomerUpdateForm;
import net.lab1024.sa.admin.module.business.roland.domain.form.RolandRegisterForm;
import net.lab1024.sa.admin.module.business.roland.domain.vo.RolandCustomerVO;
import net.lab1024.sa.admin.module.business.roland.domain.vo.RolandRegisterVO;
import net.lab1024.sa.common.common.code.UserErrorCode;
import net.lab1024.sa.common.common.domain.PageResult;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.common.util.SmartBeanUtil;
import net.lab1024.sa.common.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户信息表 Service
 *
 * @Author dragon
 * @Date 2023-12-11 13:27:12
 * @Copyright dragon
 */
@Slf4j
@Service
public class RolandCustomerService {

    @Autowired
    private RolandCustomerDao rolandCustomerDao;
    @Autowired
    private RolandOrderDao rolandOrderDao;
    @Autowired
    private RolandPerformanceStudentDao rolandPerformanceStudentDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<RolandCustomerVO> queryPage(RolandCustomerQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<RolandCustomerVO> list = rolandCustomerDao.queryPage(page, queryForm);
        PageResult<RolandCustomerVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(RolandCustomerAddForm addForm) {
        LambdaQueryWrapper<RolandCustomerEntity> qw = Wrappers.lambdaQuery();
        qw.eq(RolandCustomerEntity::getStudentName, addForm.getStudentName());
        if (rolandCustomerDao.selectCount(qw) > 0) {
            return ResponseDTO.error(UserErrorCode.CUSTOMER_DUPLICATE);
        }

        RolandCustomerEntity rolandCustomerEntity = SmartBeanUtil.copy(addForm, RolandCustomerEntity.class);
        rolandCustomerDao.insert(rolandCustomerEntity);

        for (Integer performanceTypeId : addForm.getPerformanceTypeIds()) {
            RolandPerformanceStudentEntity v = new RolandPerformanceStudentEntity();
            v.setPerformanceTypeId(performanceTypeId);
            v.setCustomerId(rolandCustomerEntity.getId());
            rolandPerformanceStudentDao.insert(v);
        }

        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(RolandCustomerUpdateForm updateForm) {
        LambdaQueryWrapper<RolandCustomerEntity> qw = Wrappers.lambdaQuery();
        qw.eq(RolandCustomerEntity::getStudentName, updateForm.getStudentName());
        qw.ne(RolandCustomerEntity::getId, updateForm.getId());
        if (rolandCustomerDao.selectCount(qw) > 0) {
            return ResponseDTO.error(UserErrorCode.CUSTOMER_DUPLICATE);
        }

        RolandCustomerEntity rolandCustomerEntity = SmartBeanUtil.copy(updateForm, RolandCustomerEntity.class);
        rolandCustomerDao.updateById(rolandCustomerEntity);

        LambdaQueryWrapper<RolandPerformanceStudentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RolandPerformanceStudentEntity::getCustomerId, updateForm.getId());
        rolandPerformanceStudentDao.delete(queryWrapper);

        for (Integer performanceTypeId : updateForm.getPerformanceTypeIds()) {
            RolandPerformanceStudentEntity v = new RolandPerformanceStudentEntity();
            v.setPerformanceTypeId(performanceTypeId);
            v.setCustomerId(updateForm.getId());
            rolandPerformanceStudentDao.insert(v);
        }

        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        rolandCustomerDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        rolandCustomerDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<RolandRegisterVO> register(RolandRegisterForm param) {

        LambdaQueryWrapper<RolandCustomerEntity> qw = Wrappers.lambdaQuery();
        qw.eq(RolandCustomerEntity::getStudentName, param.getStudentName());
        List<RolandCustomerEntity> customerList = rolandCustomerDao.selectList(qw);

        if (CollectionUtils.isEmpty(customerList)) {
            log.error("customer not exists, studentName: {}", param.getStudentName());
            return ResponseDTO.error(UserErrorCode.CUSTOMER_NOT_EXIST);
        }

        RolandCustomerEntity customer = customerList.get(0);
        // 限购票数
        int totalNum = customer.getTotalNum();

        // 查看当前客户之前订单
        LambdaQueryWrapper<RolandOrderEntity> orderQw = Wrappers.lambdaQuery();
        orderQw.eq(RolandOrderEntity::getCustomerId, customer.getId());
        orderQw.in(RolandOrderEntity::getOrderStatus, RolandOrderEntity.STATUS_LIST);
        List<RolandOrderEntity> orderList = rolandOrderDao.selectList(orderQw);
        if (CollectionUtils.isNotEmpty(orderList)) {
            List<Integer> orderIds = orderList.stream().map(RolandOrderEntity::getId).collect(Collectors.toList());
            int seatNum = rolandOrderDao.selectSeatNum(orderIds);
            totalNum -= seatNum;
        }

        return ResponseDTO.ok(new RolandRegisterVO(totalNum, customer.getId()));
    }
}
